import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadPoolDemo10 {
    public static void main(String[] args) {
        ThreadFactory factory=new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread=new Thread(r);
                return thread;
            }
        };
        //手动方式创建线程池
        ThreadPoolExecutor executor=new ThreadPoolExecutor(2,2,10,
                TimeUnit.SECONDS,new LinkedBlockingDeque<>(2),
                factory,new ThreadPoolExecutor.CallerRunsPolicy());
        for (int i = 0; i < 5; i++) {
            int finalI=i;
         executor.submit(()->{
             try {
                 Thread.sleep(100*finalI);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
             System.out.println(Thread.currentThread().getName()+"执行任务:"+finalI);
         });
        }
    }
}
